package io.renren.modules.sqlapi.service.impl;

import cn.hutool.db.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.constant.Constant;
import io.renren.common.utils.Result;
import io.renren.modules.sqlapi.SqlUtil;
import io.renren.modules.sqlapi.dao.SysApiSqlDao;
import io.renren.modules.sqlapi.dto.SysApiSqlDTO;
import io.renren.modules.sqlapi.entity.SysApiSqlEntity;
import io.renren.modules.sqlapi.service.SysApiSqlService;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.sys.dao.SysCommDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * sqlApi
 *
 * @author Mark sunlightcs@gmail.com
 * @since 3.0 2023-04-28
 */
@Service
public class SysApiSqlServiceImpl extends CrudServiceImpl<SysApiSqlDao, SysApiSqlEntity, SysApiSqlDTO> implements SysApiSqlService {
    @Autowired
    private SysCommDao sysCommDao;
    @Override
    public QueryWrapper<SysApiSqlEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<SysApiSqlEntity> wrapper = new QueryWrapper<>();


        return wrapper;
    }

    /**
     * 通过编码查询
     * @aram code 接口
     * */
    public SysApiSqlEntity queryByCode(String code ){
        QueryWrapper<SysApiSqlEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("code",code);
        List<SysApiSqlEntity> list = this.baseDao.selectList(wrapper);
        if(list.size()>0){
            return  list.get(0);
        }else{
            return null;
        }
    }

    public String querySql(String code ,Map<String,Object> param ){
        SysApiSqlEntity sqlEntity = this.queryByCode(code);
        String sql = SqlUtil.ifSql(sqlEntity.getSqlcn(),param);
        return sql;
    }
    public String querySql( SysApiSqlEntity sqlEntity ,String code ,Map<String,Object> param ){
        String sql = SqlUtil.ifSql(sqlEntity.getSqlcn(),param);
        return sql;
    }

    public PageData<Map> pageData(Page page,String code , Map<String,Object> param ){
        SysApiSqlEntity sqlEntity = this.queryByCode(code);
        String sql =querySql(sqlEntity,code,param);
        List<Map<String,Object>> list = sysCommDao.page(page,sql,param);
        if(list.size()>0 && StringUtils.isNotBlank(sqlEntity.getChilds())){
            String child = sqlEntity.getChilds();
            for(Map item : list){
                String[] childArray = child.split(":");
                String itemCode = childArray[0];
                Object value = item.get(childArray[1]);
                if(value !=null ){
                    Map<String,Object> itemParam = new HashMap<>();
                    itemParam.put(childArray[2],value);
                    if(childArray.length>=4){
                        for(int j =3;j<childArray.length;j++){
                            itemParam.put(childArray[j],param.get(childArray[j]));
                        }
                    }
                    itemParam.put("userId",param.get("userId"));
                    itemParam.put("user",param.get("user"));
                    if("matchRecordItem".equals(itemCode)) {
                        Object userId = param.get("userId");
                        if (userId == null) {
                            itemParam.put("noHistory", "1");
                        }
                    }
                    String itemSql =querySql(itemCode,itemParam);
                    List itemList = sysCommDao.commList(itemSql,itemParam);
                    item.put(childArray[1],itemList);
                }
            }
        }
        Integer total =sysCommDao.count("SELECT COUNT(1) FROM ("+sql+")a ",param);
        PageData<Map> pageData = new PageData(list,total);
        return pageData;
    }


    public List<Map<String, String>> list(String code , Map<String,Object> param ){
        SysApiSqlEntity sqlEntity = this.queryByCode(code);
        String sql =querySql(sqlEntity,code,param);
        List<Map<String, String>> list = sysCommDao.commList(sql,param);
        if(list.size()>0 && StringUtils.isNotBlank(sqlEntity.getChilds())){
            String child = sqlEntity.getChilds();
            for(Map item : list){
                String[] childArray = child.split(":");
                String itemCode = childArray[0];
                Object value = item.get(childArray[1]);
                if(value !=null ){
                    Map<String,Object> itemParam = new HashMap<>();
                    itemParam.put(childArray[2],value);
                    itemParam.put("user",param.get("user"));
                    if(childArray.length>=4){
                        itemParam.put(childArray[3],param.get(childArray[3]));
                    }
                    String itemSql =querySql(itemCode,itemParam);
                    List itemList = sysCommDao.commList(itemSql,itemParam);
                    item.put(childArray[1],itemList);
                }
            }
        }
        return list;
    }


}
